﻿/********************************************************************************************
 * 文件名称:	Add.cs
 * 设计人员:	严向晖(yanxianghui@gmail.com)
 * 设计时间:	2009-04-10
 * 功能描述:	添加豆列
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		    修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.UI.WebControls;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.Model;
using BLL = Shanfree.SharpMovie.BLL;
using SIRC.Framework.Net;

public partial class List_Add : BLL.BasePage
{
    /// <summary>
    /// 列表的主键
    /// </summary>
    public string ListID
    {
        get
        {
            return GetViewState("ListID");
        }
        set
        {
            ViewState["ListID"] = value;
        }
    }
    /// <summary>
    /// 需要后台登陆
    /// </summary>
    protected override bool IsAuth
    {
        get
        {
            return false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                string listID = GetRequest("listid");
                ddlMenu.DataSource = new BLL.Menu().GetSubMenuList("8");
                ddlMenu.DataBind();

                if (string.IsNullOrEmpty(listID))
                {
                    // 添加
                    trCreateTime.Visible = false;
                    trList.Visible = false;
                }
                else
                {
                    // 编辑
                    this.ListID = listID;
                    ListInfo lInfo = new BLL.List().GetByID(listID);
                    tbListName.Text = lInfo.Name;
                    // 绑定栏目
                    ddlMenu.SelectedValue = lInfo.CatalogID;
                    tbListRemark.Text = lInfo.Remark;
                    lbAmount.Text = lInfo.Amount.ToString();
                    tbTotal.Text = lInfo.Total.ToString();
                    tbListCreateTime.Text = lInfo.CreateTime.ToShortDateString();
                    int pageSize = 12;
                    int pageIndex = 0;
                    BindList(listID, pageSize, pageIndex);
                }
            }
        }
        catch (Exception ex)
        {
            ShowMsg(ex.Message);
            LogEntry.Log.Write(ex.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }

    private void BindList(string listID, int pageSize, int pageIndex)
    {
        IList<ListItemInfo> itemList = new BLL.List().GetItemList(listID, 12, 0);
        // 增加一条空记录
        itemList.Add(new ListItemInfo());
        gvList.DataSource = itemList;
        gvList.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        // 我的收藏中默认添加一影片
        string movieID = GetRequest("movieid");
        string name = tbListName.Text;
        string remark = tbListRemark.Text;
        string total = tbTotal.Text;
        string menuID = ddlMenu.SelectedValue;
        string listID = new BLL.List().Add(name, remark, total, menuID, this.CurrentUser.ID);
        this.ListID = listID;
    }

    #region 列表项的操作
    protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string ID = e.CommandArgument.ToString();
            int index = Convert.ToInt32(ID);
            GridViewRow gvr = (GridViewRow)gvList.Rows[index];
            TextBox tbIMDB = (TextBox)gvr.FindControl("tbIMDB");
            Label lblID = (Label)gvr.FindControl("lblID");
            if (e.CommandName == "btnDel")
            {
                new BLL.ListItem().Delete(lblID.Text);
            }
            else
            {
                #region 控件
                TextBox tbListRemark = (TextBox)gvr.FindControl("tbListRemark");
                TextBox tbSortIndex = (TextBox)gvr.FindControl("tbSortIndex");
                int sortIndex = Convert.ToInt32(tbSortIndex.Text);
                #endregion

                if (e.CommandName == "btnEdit")
                {
                    MovieInfo mInfo = new BLL.Movie().GetByID(lblID.Text);
                    new BLL.ListItem().Update(mInfo.ID, tbListRemark.Text, sortIndex);
                }

                if (e.CommandName == "btnAdd")
                {
                    bool isExist = new BLL.Movie().Exist(tbIMDB.Text);
                    string movieID = string.Empty;
                    if (isExist == false)
                    {
                        // 从imdb网站导入
                        movieID = new BLL.Movie().Import(tbIMDB.Text, this.CurrentUser.ID);
                    }
                    new BLL.ListItem().Add(this.ListID, movieID, tbListRemark.Text);
                }
            }
            int pageIndex = 0;
            BindList(this.ListID, gvList.PageSize, pageIndex);
            ShowMsg("操作成功！");
        }
        catch (Exception exc)
        {
            ShowMsg(exc.Message);
            LogEntry.Log.Write(exc.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnAdd = (Button)e.Row.FindControl("btnAdd");
            Button btnEdit = (Button)e.Row.FindControl("btnEdit");
            Button btnDel = (Button)e.Row.FindControl("btnDel");
            TextBox tbIMDB = (TextBox)e.Row.FindControl("tbIMDB");
            Label lbIMDB = (Label)e.Row.FindControl("lbIMDB");

            if (tbIMDB == null || string.IsNullOrEmpty(tbIMDB.Text))
            {
                btnAdd.Visible = true;
                lbIMDB.Visible = false;
                tbIMDB.Visible = true;
                btnEdit.Visible = false;
                btnDel.Visible = false;
            }
            else
            {
                lbIMDB.Visible = true;
                tbIMDB.Visible = false;
                btnAdd.Visible = false;
                btnEdit.Visible = true;
                btnDel.Visible = true;
            }
        }
    }
    #endregion
}
